package com.example.javawebapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class BigDataTestApplication {
    private JdbcTemplate jdbcTemplate;
    @Autowired
    public void Hello(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public BigDataTestApplication(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Value("${spring.datasource.url}")
    public String dbUrl;

    public static void main(String[] args) {

        SpringApplication.run(BigDataTestApplication.class, args);
    }
    @GetMapping("/api")
    public String hello() {
        return "Hello, World!hahahaha"+dbUrl;
    }

    @GetMapping("/api/rowCount")
    public String getRowCount(@RequestParam(required = false,
            defaultValue = "sg_article") String tableName) {
        String sql = String.format("SELECT COUNT(*) FROM %s",
                tableName);
        Integer rowCount = jdbcTemplate.queryForObject(sql,
                Integer.class);
        if (rowCount == null) {
            return "Row count: 0"; // 处理null情况
        }
        return "Row count: " + rowCount;
    }
}